package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.cache.prefix.UserInfoKeyPrefix;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static void addCookie(HttpServletResponse response, String token) {
        Cookie cookie = new Cookie(UserInfoKeyPrefix.USER_LOGIN_COOKIE_NAME, token);
        cookie.setPath("/");
        cookie.setMaxAge(UserInfoKeyPrefix.USER_INFO_TOKEN.getTime());
        response.addCookie(cookie);
    }

    /**
     * 获取Cookie
     * @param request
     * @param cookieName
     * @return
     */
    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equalsIgnoreCase(cookieName)){
                    return cookie;
                }
            }
        }
        return null;
    }
}
